/*
 * Package il.ac.biu.cs.grossmm.api.keys
 * File Parameter.java
 * Created on Jul 25, 2006
 *
 */
package il.ac.biu.cs.grossmm.api.keys;

/**
 * A parameter is a pattern for atomic values (java objects). 
 * The pattern is associated with a Java class. 
 * A value matches this pattern iff value is not null
 * and belongs to the Java class associated with the pattern
 * or value is null and null values are allowed by this pattern. 
 */
public class Parameter<P, V> {
    final public Class<V> clazz;
    
    public Parameter(Class<V> clazz) {
        this.clazz = clazz;
    }
    
    /**
     * Matches a value against this parameter
     * 
     * @param value a value to match agains this parameter
     * @return true iff matches
     */
    public boolean matches(Object value) {
        return value == null || clazz.isAssignableFrom(value.getClass());
    }
    
    @Override
    public boolean equals(Object obj) {
        if( obj instanceof Parameter ) {
            Parameter p = (Parameter) obj;
            
            return clazz.equals(p.clazz); 
        }
        
        return false;
    }
    
    @Override
    public int hashCode() {
        return 0xd0defeed ^ clazz.hashCode();
    }
}
